﻿using Ping9719.DeviceCommunication;
using Ping9719.WpfEx;
using SqlSugar;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;
using System.Windows.Interop;
using System.Windows.Markup;
using static BaseUi.Db.Models.MESModels;

namespace BaseUi.Db.Models
{ /// <summary>
  /// 全线产品数据
  /// </summary>
    public class MESModels
    {
        [SugarTable("HTZD_Assemble", TableDescription = "全线产品数据")]
        public class HTZD_Assemble
        {
            [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]//自增主键
            public long id { get; set; }
            public string barCode { get; set; }
            public string LaserEtchingStatus { get; set; }
            public string LaserEtchingResult { get; set; }
            public DateTime LaserEtchingTime { get; set; }
            public string LaserEtchingTrackNo { get; set; }
            public string WriteCodeStatus { get; set; }
            public string WriteCodeResult { get; set; }
            public DateTime WriteCodeTime { get; set; }
            public string WriteCodeTrackNo { get; set; }
            public string WriteCodeDevcode { get; set; }
            public string PlatingStatus { get; set; }
            public DateTime PlatingTime { get; set; }
            public string moCode { get; set; }
            public string Printingname { get; set; }
            public DateTime PrintingTime { get; set; }
            public string Recordingname { get; set; }
            public DateTime RecordingTime { get; set; }

            public string TrayLayerNo { get; set; }
            public string TrayNo { get; set; }
        }
        [SugarTable("HTZD_Plating", TableDescription = "下料站数据")]
        public class HTZD_Plating
        {
            /// <summary>
            /// 
            /// </summary>
            [SugarColumn(IsIdentity = true, IsPrimaryKey = true)]
            public long id { get; set; }
            public string barCode { get; set; }
            public string LocationNo { get; set; }
            public string TrayLayerNo { get; set; }
            public string TrayNo { get; set; }
            public string Result { get; set; }
            public DateTime createTime { get; set; }
            public string userName { get; set; }
            public string moCode { get; set; }
        }
        [SugarTable("HTZD_Barcode_base", TableDescription = "工单表")]
        public class HTZD_Barcode_base
        {
            /// <summary>
            /// 
            /// </summary>
            [SugarColumn(IsIdentity = true, IsPrimaryKey = true)]
            public long id { get; set; }
            public string barCode { get; set; }
            public string moCode { get; set; }
            public DateTime createTime { get; set; }
            public bool isUse { get; set; }
            public DateTime useTime { get; set; }
        }
        /// <summary>
        /// 如果没有待供料则认为是新一轮，将最小的30个改为待供料
        /// </summary>
        /// <returns></returns>
        public static string GetInfo(bool check)
        {
            string ErrInfo = "";

            DataDb.db.BeginTran();
            try
            {
                List<HTZD_Assemble> list = DataDb.db.Queryable<HTZD_Assemble>().Where(it => it.PlatingStatus == "待供料").ToList();
                if (list.Count == 0)
                {
                    List<HTZD_Assemble> cahn = DataDb.db.Queryable<HTZD_Assemble>().Where(it => it.PlatingStatus == "供料").ToList();
                    if (cahn.Count != 0)
                    {
                        return $"待供料的数量为0，供料的数量不为0";
                    }
                    HTZD_Assemble fisrt = DataDb.db.Queryable<HTZD_Assemble>().Where(it => it.PlatingStatus != null).OrderBy(it => it.id, OrderByType.Desc).First();
                    List<HTZD_Assemble> list1 = DataDb.db.Queryable<HTZD_Assemble>().Where(it => it.PlatingStatus == null).OrderBy(it => it.id, OrderByType.Asc).Take(30).ToList();
                    if (list1.Count!=30)
                    {
                        return $"数据库未摆盘的数据只有{list1.Count}";
                    }
                    if (fisrt != null)
                    {
                        if (fisrt.moCode== list1[0].moCode)
                        {
                            string getduibi = duibi(fisrt.barCode, list1[0].barCode, check);
                            if (getduibi != "OK")
                            {
                                return getduibi;
                            }
                        }
                    }
                    foreach (var item in list1)
                    {
                        item.PlatingStatus = "待供料";
                        //DataDb.db.Updateable<HTZD_Assemble>().SetColumns(it => it.PlatingStatus == "待供料").Where(it => it.barCode == list1[i].barCode).ExecuteCommand();
                    }
                    int count = DataDb.db.Updateable(list1).ExecuteCommand();
                    if (count!=30)
                    {
                        DataDb.db.RollbackTran();
                        return "更新待供料失败";
                    }

                    DataDb.db.CommitTran();
                    return "OK";
                }
                if (list.Count != 0&& list.Count != 15&& list.Count != 30)
                {
                    ErrInfo = $"待供料的产品数量为{list.Count},数量异常";
                }
                else
                {
                    return "OK";
                }
            }
            catch (Exception ee)
            {
                DataDb.db.RollbackTran();
                Logs.Write(" 整线查询到料错误代码异常：" + ee.Message + ";");
                ErrInfo = "失败:查询错误代码异常";
            }
            return ErrInfo;
        }

        private static string duibi(string barCode1, string barCode2, bool check, int dubi = 1)
        {
            string temp = "";
            try
            {
                if (check == true)
                {
                    //序列号最后1位是校验位
                    Int64 x = Convert.ToInt64(barCode1.Substring(barCode1.Length - 8, 7));
                    Int64 y = Convert.ToInt64(barCode2.Substring(barCode2.Length - 8, 7));
                    if (y - x == dubi)
                    {
                        temp = "OK";
                    }
                    else
                    {
                        temp = "序列号不连续有校验位";
                    }
                }
                else
                {
                    //序列号没有校验位
                    Int64 x = Convert.ToInt64(barCode1.Substring(barCode1.Length - 7, 7));
                    Int64 y = Convert.ToInt64(barCode2.Substring(barCode2.Length - 7, 7));
                    if (y - x == dubi)
                    {
                        temp = "OK";
                    }
                    else
                    {
                        temp = "序列号不连续没有校验位";
                    }
                }
            }
            catch (Exception ex)
            {
                temp = "检查序号连续性失败：" + ex.Message;
                Logs.Write("检查序号连续性失败：" + ex.Message);
            }
            return temp;
        }

        /// <summary>
        /// 判断到位的是否是15个，是否连续，是30个的前15还是后15将该皮带上的15个待供料改为供料
        /// </summary>
        /// <param name="trackNo"></param>
        /// <returns></returns>
        public static string UpdatafsInfo(string trackNo)
        {
            DataDb.db.BeginTran();
            string ErrInfo = "";
            try
            {
                List<HTZD_Assemble> list1 = DataDb.db.Queryable<HTZD_Assemble>().Where(it => it.PlatingStatus != null).OrderBy(it => it.id, OrderByType.Desc).Take(30).ToList();

                List<HTZD_Assemble> list2 = DataDb.db.Queryable<HTZD_Assemble>().Where(it => it.PlatingStatus == "待供料" && it.WriteCodeTrackNo==trackNo).OrderBy(it => it.id, OrderByType.Desc).ToList();
                if (list2.Count!=15)
                {
                    return $"失败:供料皮带{trackNo}的到料数量为{list2.Count}";
                }
                if (list2[0].barCode!= list1[0].barCode&& list2[0].barCode != list1[15].barCode)
                {
                    return $"失败:供料皮带的到料最小为{list2[14].barCode};最大为{list2[0].barCode},该盘最小为{list1[29].barCode};最大为{list1[0].barCode}";

                }
                if (list2[0].barCode != list1[0].barCode)
                {
                    ErrInfo= $"成功:1:由{list2[0].barCode}到{list2[14].barCode}";
                }
                else
                {
                    ErrInfo = $"成功:2:由{list2[0].barCode}到{list2[14].barCode}";
                }

                foreach (var item in list2)
                {
                    item.PlatingStatus = "供料";
                    //DataDb.db.Updateable<HTZD_Assemble>().SetColumns(it => it.PlatingStatus == "抓取").Where(it => it.barCode == list1[i].barCode).ExecuteCommand();
                }
                int count = DataDb.db.Updateable(list2).ExecuteCommand();
                if (count != 15)
                {
                    DataDb.db.RollbackTran();
                    ErrInfo= "失败:更新待供料失败";
                }
                else
                {
                    DataDb.db.CommitTran();
                }
            }
            catch (Exception ee)
            {
                DataDb.db.RollbackTran();
                Logs.Write(" 整线查询到料错误代码异常：" + ee.Message + ";");
                ErrInfo = "失败:查询错误代码异常";
            }
            return ErrInfo;
        }
        /// <summary>
        /// 如果没有待供料则认为是新一轮，将最小的30个改为待供料
        /// </summary>
        /// <returns></returns>
        public static string MLGetInfo1(string TrackNo)
        {
            int trackno = int.Parse(TrackNo);
            string ErrInfo = "";
            DataDb.db.BeginTran();
            try
            {
                List<HTZD_Assemble> list = DataDb.db.Queryable<HTZD_Assemble>().Where(it => it.PlatingStatus == "待供料").ToList();
                if (list.Count != 0)
                {
                    ErrInfo = $"待供料的产品数量为{list.Count},数量异常";
                    return ErrInfo;
                }
                List<HTZD_Assemble> list4 = DataDb.db.Queryable<HTZD_Assemble>().Where(it => it.PlatingStatus == "供料").ToList();
                if (list4.Count >=15)
                {
                    return "OK";
                }
                if ((list4.Count < 15&& list4.Count > 0)|| list4.Count > 30)
                {
                    ErrInfo = $"供料的产品数量为{list.Count},数量异常";
                    return ErrInfo;
                }
                List<HTZD_Assemble> list1 = new List<HTZD_Assemble>();
                for (int i = 0; i < 30; i++)
                {
                    HTZD_Assemble one = new HTZD_Assemble();
                    one.PlatingStatus = "供料";
                    one.WriteCodeResult = "OK";
                    if (i <15)
                    {
                        one.WriteCodeTrackNo="1";
                    }
                    else
                    {
                        one.WriteCodeTrackNo="2";
                    }
                    one.barCode = (12345968123+ i+(trackno*30)).ToString();
                    list1.Add(one);
                }
                int count = DataDb.db.Insertable(list1).ExecuteCommand();
                if (count!=30)
                {
                    DataDb.db.RollbackTran();
                    return "失败:待供料插入失败";
                }
                DataDb.db.CommitTran();
                return "OK";

            }
            catch (Exception ee)
            {
                DataDb.db.RollbackTran();
                Logs.Write(" 模拟整线查询到料错误代码异常：" + ee.Message + ";");
                ErrInfo = "失败:模拟查询错误代码异常";
            }
            return ErrInfo;
        }
        public static string MLGetInfo(string TrackNo)
        {
            int trackno = int.Parse(TrackNo);
            string ErrInfo = "";
            DataDb.db.BeginTran();
            try
            {
                List<HTZD_Assemble> list1 = new List<HTZD_Assemble>();
                List<HTZD_Assemble> list = DataDb.db.Queryable<HTZD_Assemble>().Where(it => it.PlatingStatus == "待供料").ToList();
                if (list.Count != 0)
                {
                    if (list.Count != 15)
                    {
                        ErrInfo = $"总待供料的产品数量为{list.Count},数量异常";
                        return ErrInfo;
                    }

                    List<HTZD_Assemble> list5 = DataDb.db.Queryable<HTZD_Assemble>().Where(it => it.PlatingStatus == "待供料"&&it.WriteCodeTrackNo== TrackNo).ToList();
                    if (list5.Count != 15)
                    {
                        ErrInfo = $"该轨道待供料的产品数量为{list5.Count},数量异常";
                        return ErrInfo;
                    }
                    else
                    {
                        for (int i = 0; i<15; i++)
                        {
                            list5[i].PlatingStatus = "供料";
                        }
                        int count = DataDb.db.Updateable(list5).ExecuteCommand();
                        if (count != 15)
                        {
                            DataDb.db.RollbackTran();
                            ErrInfo = "失败:更新待供料失败";
                            return ErrInfo;
                        }
                        else
                        {
                            DataDb.db.CommitTran();
                            return "OK";
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < 30; i++)
                    {
                        HTZD_Assemble one = new HTZD_Assemble();
                        one.PlatingStatus = "待供料";
                        one.WriteCodeResult = "OK";
                        if (i < 15)
                        {
                            one.WriteCodeTrackNo = "1";
                            if (TrackNo=="1")
                            {
                                one.PlatingStatus = "供料";
                            }
                        }
                        else
                        {
                            one.WriteCodeTrackNo = "2";
                            if (TrackNo == "2")
                            {
                                one.PlatingStatus = "供料";
                            }
                        }
                        one.barCode = (12345968123 + i).ToString();
                        list1.Add(one);
                    }
                    int count = DataDb.db.Insertable(list1).ExecuteCommand();
                    if (count != 30)
                    {
                        DataDb.db.RollbackTran();
                        return "失败:待供料插入失败";
                    }
                    DataDb.db.CommitTran();
                    return "OK";
                }
            }
            catch (Exception ee)
            {
                DataDb.db.RollbackTran();
                Logs.Write(" 模拟整线查询到料错误代码异常：" + ee.Message + ";");
                ErrInfo = "失败:模拟查询错误代码异常";
            }
            return ErrInfo;
        }
        /// <summary>
        /// 判断到位的是否是15个，是否连续，是30个的前15还是后15将该皮带上的15个待供料改为供料
        /// </summary>
        /// <param name="trackNo"></param>
        /// <returns></returns>
        public static string MLUpdatafsInfo(string trackNo)
        {
            string ErrInfo = "";
            try
            {
                List<HTZD_Assemble> list1 = DataDb.db.Queryable<HTZD_Assemble>().Where(it => it.PlatingStatus == null).OrderBy(it => it.id, OrderByType.Asc).Take(30).ToList();

            }
            catch (Exception ee)
            {
                Logs.Write(" 整线查询到料错误代码异常：" + ee.Message + ";");
                ErrInfo = "失败:查询错误代码异常";
            }
            return ErrInfo;
        }
        /// <summary>
        /// 将5个OK/NG获取
        /// </summary>
        /// <returns></returns>
        internal static string getmoveinfo(int data1)
        {
            string result = "";
            int vum = 30 - (data1 * 5);
            try
            {
                List<HTZD_Assemble> liststart = DataDb.db.Queryable<HTZD_Assemble>().Where(it => it.PlatingStatus == "供料" || it.PlatingStatus == "待供料").ToList();
                if (liststart.Count != vum)
                {
                    result = $"错误:未抓取的数量为{liststart.Count},数量错误";
                    return result;
                }
                List<HTZD_Assemble> list1 = DataDb.db.Queryable<HTZD_Assemble>().Where(it => it.PlatingStatus == "供料").OrderBy(it => it.id, OrderByType.Asc).Take(5).ToList();
                if (list1.Count!=5)
                {
                    result= $"错误:供料的数量为{list1.Count},数量错误";
                    return result;
                }
                int value = 0;
                string jg = "";
                for (int i = 0; i< list1.Count; i++)
                {
                    if (list1[i].WriteCodeResult == "OK")
                        value = value + (0x01 << i);
                    jg = jg + "\r\n" + list1[i].barCode+"-" + list1[i].WriteCodeResult;
                }
                int num = list1.Where(it => it.WriteCodeResult != "OK").Count();
                if (num==5)
                {
                    value=-1;
                }
                result = $"成功:{value}:{jg}";
                return result;
            }
            catch (Exception ee)
            {
                result = "错误:" + ee.Message;
            }
            return result;
        }

        internal static string updatamoveinfo(bool check)
        {
            string result = "";
            DataDb.db.BeginTran();
            try
            {

                //HTZD_Plating list4 = DataDb.db.Queryable<HTZD_Plating>().OrderBy(it => it.id, OrderByType.Desc).First();
                //List<HTZD_Plating> list = new List<HTZD_Plating>();
                List<HTZD_Assemble> list1 = DataDb.db.Queryable<HTZD_Assemble>().Where(it => it.PlatingStatus == "供料").OrderBy(it => it.id, OrderByType.Asc).Take(5).ToList();
                HTZD_Assemble list5 = DataDb.db.Queryable<HTZD_Assemble>().Where(it => it.PlatingStatus != "供料"&&it.PlatingStatus != "待供料").OrderBy(it => it.id, OrderByType.Desc).First();
                if (list1.Count!=5)
                {
                    result = $"错误:供料的数量为{list1.Count},数量错误";
                    return result;
                }
                if (list5!=null)
                {
                    if (list5.moCode == list1[0].moCode)
                    {
                        string getduibi = duibi(list5.barCode, list1[0].barCode, check);
                        if (getduibi != "OK")
                        {
                            return getduibi;
                        }
                    }
                }
                for (int i = 0; i< list1.Count; i++)
                {

                    //HTZD_Plating plat = new HTZD_Plating();
                    //plat.barCode = list1[i].barCode;
                    //if (list1[i].WriteCodeResult=="OK")
                    //{
                    //    plat.Result = list1[i].WriteCodeResult;
                    //}
                    //else
                    //{
                    //    plat.Result = "NG";
                    //}
                    //plat.moCode = list1[i].moCode;
                    //plat.createTime = DateTime.Now;
                    //list.Add(plat);
                    list1[i].PlatingTime = DateTime.Now;
                    list1[i].PlatingStatus = "抓取";
                }
                //int count = DataDb.db.Insertable(list).ExecuteCommand();
                //if (count == list1.Count)
                //{
                int count = DataDb.db.Updateable(list1).ExecuteCommand();
                if (count == list1.Count)
                {
                    DataDb.db.CommitTran();
                    return "成功";
                }
                DataDb.db.RollbackTran();
                return "错误:更新全线数据库失败";
                //}
                //else
                //{
                //    DataDb.db.RollbackTran();
                //    return "错误:写入下料本地数据库失败";
                //}
                DataDb.db.CommitTran();
            }
            catch (Exception ee)
            {
                DataDb.db.RollbackTran();
                result = "错误:" + ee.Message;
            }
            return result;
        }
        internal static string mlupdatamoveinfo(bool check)
        {
            string result = "";
            DataDb.db.BeginTran();
            try
            {

                //HTZD_Plating list4 = DataDb.db.Queryable<HTZD_Plating>().OrderBy(it => it.id, OrderByType.Desc).First();
                //List<HTZD_Plating> list = new List<HTZD_Plating>();
                List<HTZD_Assemble> list1 = DataDb.db.Queryable<HTZD_Assemble>().Where(it => it.PlatingStatus == "供料").OrderBy(it => it.id, OrderByType.Asc).Take(5).ToList();
                if (list1.Count!=5)
                {
                    result = $"错误:供料的数量为{list1.Count},数量错误";
                    return result;
                }
                //if (list4!=null)
                //{
                //    string getduibi = duibi(list4.barCode, list1[0].barCode, check);
                //    if (getduibi != "OK")
                //    {
                //        return getduibi;
                //    }
                //}
                for (int i = 0; i< list1.Count; i++)
                {
                    //HTZD_Plating plat = new HTZD_Plating();
                    //plat.barCode = list1[i].barCode;
                    //plat.Result = list1[i].WriteCodeResult;
                    //list.Add(plat);
                    list1[i].PlatingStatus = "抓取";
                }
                //int count = DataDb.db.Insertable(list).ExecuteCommand();
                //if (count == list1.Count)
                //{
                int count = DataDb.db.Updateable(list1).ExecuteCommand();
                if (count == list1.Count)
                {
                    DataDb.db.CommitTran();
                    return "成功";
                }
                DataDb.db.RollbackTran();
                return "错误:更新全线数据库失败";
                //}
                //else
                //{
                //    DataDb.db.RollbackTran();
                //    return "错误:写入下料本地数据库失败";
                //}
                //DataDb.db.CommitTran();
            }
            catch (Exception ee)
            {
                DataDb.db.RollbackTran();
                result = "错误:" + ee.Message;
            }
            return result;
        }
        /// <summary>
        /// 整线的产品没有供料和待供料的。
        /// </summary>
        /// <returns></returns>
        internal static string confirmmoveinfo()
        {
            string result = "";
            try
            {
                List<HTZD_Assemble> list1 = DataDb.db.Queryable<HTZD_Assemble>().Where(it => it.PlatingStatus == "供料" || it.PlatingStatus == "待供料").ToList();
                if (list1.Count!=0)
                {
                    result = "错误:待供料或供料的数量不为0";
                    return result;
                }
                return "成功";
            }
            catch (Exception ee)
            {
                result = "错误:" + ee.Message;
            }
            return result;
        }
        /// <summary>
        /// 将全线产品的状态由待下料改为下料
        /// </summary>
        /// <returns></returns>
        internal static string confirmbaitinginfo()
        {
            DataDb.db.BeginTran();
            string result = "";
            try
            {
                List<HTZD_Assemble> list1 = DataDb.db.Queryable<HTZD_Assemble>().Where(it => it.PlatingStatus == "待下料").OrderBy(it => it.id, OrderByType.Asc).ToList();
                for (int i = 0; i < list1.Count; i++)
                {
                    //HTZD_Plating plat = new HTZD_Plating();
                    //plat.barCode = list1[i].barCode;
                    //plat.Result = list1[i].WriteCodeResult;
                    list1[i].PlatingStatus = "下料";
                }
                int count = DataDb.db.Updateable(list1).ExecuteCommand();
                if (count!= list1.Count)
                {
                    DataDb.db.RollbackTran();
                    return "错误:tray堆下料数据库更新失败";
                }
                DataDb.db.CommitTran();
                return "成功";
            }
            catch (Exception ee)
            {
                DataDb.db.RollbackTran();
                result = "错误:" + ee.Message;
            }
            return result;
        }

        /// <summary>
        /// 将全线产品的状态由tray确认改为待下料，看tray号是否需要加1
        /// </summary>
        /// <returns></returns>
        internal static string pileinfo(bool MLbool)
        {
            DataDb.db.BeginTran();
            string TrayLayerNonew = "";
            string TrayNonew = "";
            string result = "";
            try
            {
                HTZD_Assemble list1 = DataDb.db.Queryable<HTZD_Assemble>().Where(it => it.PlatingStatus == "待下料").OrderBy(it => it.id, OrderByType.Asc).First();
                List<HTZD_Assemble> list2 = DataDb.db.Queryable<HTZD_Assemble>().Where(it => it.PlatingStatus == "tray盘确认").OrderBy(it => it.id, OrderByType.Asc).ToList();
                //List<HTZD_Assemble> list21 = DataDb.db.Queryable<HTZD_Assemble>().Where(it => it.PlatingStatus != null).ToList();
                List<HTZD_Plating> list3 = new List<HTZD_Plating>();
                if (list2.Count!=30)
                {
                    result = $"错误:tray盘的数量错误，tray盘视觉确认的有{list2.Count}个";
                    return result;
                }
                if (list1!= null)
                {
                    HTZD_Plating list4 = DataDb.db.Queryable<HTZD_Plating>().Where(it => it.TrayNo != null).OrderBy(it => it.id, OrderByType.Desc).First();

                    string[] TrayLayer = list4.TrayLayerNo.Split('-');
                    int LayerNo = int.Parse(TrayLayer[0]);
                    TrayLayerNonew = (LayerNo + 1).ToString();
                    TrayNonew = list4.TrayNo;
                }
                else
                {
                    HTZD_Plating list4 = DataDb.db.Queryable<HTZD_Plating>().Where(it => it.TrayNo != null).
                                        OrderBy(it => it.TrayNo, SqlSugar.OrderByType.Desc).First();
                    if (list4!=null)
                    {
                        //TrayNonew = list4.TrayNo;
                        string[] box = list4.TrayNo.Split('-');
                        string type = box[0];
                        int No = int.Parse(box[1]);
                        TrayNonew = type + "-" + (No + 1).ToString().PadLeft(6, '0');
                        //string[] layer = list4.TrayLayerNo.Split('-');
                        //TrayLayerNonew = (int.Parse(layer[0])+1).ToString();
                        TrayLayerNonew = "1";
                    }
                    else
                    {
                        TrayNonew = "Tray-000001";
                        TrayLayerNonew = "1";
                    }
                }
                string mse = "";
                for (int i = 0; i < list2.Count; i++)
                {
                    //List<HTZD_Plating> one = DataDb.db.Queryable<HTZD_Plating>().Where(it => it.barCode == list2[i].barCode &&it.TrayLayerNo==null).OrderBy(it => it.id, OrderByType.Asc).ToList();
                    //if (one==null)
                    //{
                    //    mse = mse + list2[i].barCode + "\r\n";
                    //    //return $"{list2[i].barCode}编号本地数据库异常";
                    //}
                    //if (one.Count!=1&&!MLbool)
                    //{
                    //    mse = mse + list2[i].barCode+"有"+one.Count+"条" + "\r\n";
                    //}
                    list2[i].PlatingStatus = "待下料";
                    list2[i].TrayLayerNo = TrayLayerNonew+"-"+(i+1).ToString();
                    list2[i].TrayNo = TrayNonew;
                    //one[0].TrayLayerNo = TrayLayerNonew+"-"+(i+1).ToString();
                    //one[0].TrayNo = TrayNonew;
                    HTZD_Plating plat = new HTZD_Plating();
                    plat.barCode = list2[i].barCode;
                    if (list2[i].WriteCodeResult=="OK")
                    {
                        plat.Result = list2[i].WriteCodeResult;
                    }
                    else
                    {
                        plat.Result = "NG";
                    }
                    plat.moCode = list2[i].moCode;
                    plat.createTime = DateTime.Now;
                    plat.TrayLayerNo = TrayLayerNonew+"-"+(i+1).ToString();
                    plat.TrayNo = TrayNonew;
                    list3.Add(plat);
                    //list3.Add(one[0]);
                }
                if (mse!="")
                {
                    return $"本地数据库异常,编号:{mse}";
                }
                int count = DataDb.db.Insertable(list3).ExecuteCommand();
                if (count!= list3.Count)
                {
                    DataDb.db.RollbackTran();
                    return $"错误:更新下料数据库tray盘信息失败";

                }
                count = DataDb.db.Updateable(list2).ExecuteCommand();
                if (count != list2.Count)
                {
                    DataDb.db.RollbackTran();
                    return $"错误:更新全线数据库待下料失败";

                }
                DataDb.db.CommitTran();
                return $"成功:tray堆号{TrayNonew}，tray层{TrayLayerNonew}";
            }
            catch (Exception ee)
            {
                DataDb.db.RollbackTran();
                result = "错误:" + ee.Message;
            }
            return result;
        }

        internal static string trayinfo()
        {
            DataDb.db.BeginTran();
            string result = "";
            try
            {
                string ms = "";
                List<HTZD_Assemble> list2 = DataDb.db.Queryable<HTZD_Assemble>().Where(it => it.PlatingStatus == "抓取").OrderBy(it => it.id, OrderByType.Asc).Take(5).ToList();
                if (list2.Count != 5)
                {
                    result = $"错误:抓取状态的数量为{list2.Count},数量错误";
                    return result;
                }
                for (int i = 0; i<5; i++)
                {
                    list2[i].PlatingStatus = "在tray盘";
                    ms = ms + "\r\n" + list2[i].barCode+","+ list2[i].WriteCodeResult+" ";
                }
                int count = DataDb.db.Updateable(list2).ExecuteCommand();
                if (count!= list2.Count)
                {
                    DataDb.db.RollbackTran();
                    return "错误:全线更新在tray盘的数据错误";
                }
                DataDb.db.CommitTran();
                return $"成功:{ms}:{list2.Count}";
            }
            catch (Exception ee)
            {
                DataDb.db.RollbackTran();
                result = "错误:" + ee.Message;
            }
            return result;
        }

        internal static string Visioninfo(string[] vision)
        {
            DataDb.db.BeginTran();
            string result = "";
            try
            {
                string ms = "";
                string msg = "";
                if (vision.Length != 30)
                {
                    return "错误:视觉查询数量错误";
                }
                List<HTZD_Assemble> list2 = DataDb.db.Queryable<HTZD_Assemble>().Where(it => it.PlatingStatus == "在tray盘").OrderBy(it => it.id, OrderByType.Asc).ToList();
                if (list2.Count != 30)
                {
                    result = $"错误:在tray盘中的数量为{list2.Count},数量错误";
                    return result;
                }
                for (int i = 0; i < 30; i++)
                {
                    int Lower = i / 5;
                    int arry = i % 5;
                    if (vision[i] == "0")
                    {
                        if (list2[i].WriteCodeResult != "NG")
                        {
                            ms = ms + "\r\n" + $"第{Lower + 1}行{arry + 1}列编码{list2[i].barCode}错误，视觉为空，数据库不是NG";
                        }
                    }
                    else
                    {
                        if (list2[i].barCode != vision[i])
                        {
                            ms = ms + "\r\n" + $"第{Lower + 1}行{arry + 1}列编码{list2[i].barCode}错误，视觉{vision[i]}编码不一";
                        }
                        if (list2[i].WriteCodeResult != "OK")
                        {
                            ms = ms + "\r\n" + $"第{Lower + 1}行{arry + 1}列编码{list2[i].barCode}错误，视觉不为空，数据库不为NG";
                        }
                    }
                    msg = msg + "\r\n" + $"第{Lower + 1}行{arry + 1}列编码{list2[i].barCode}";
                    list2[i].PlatingStatus = "tray盘确认";
                }
                if (ms!="")
                {
                    result = "错误:" + ms;
                    return result;
                }
                int count = DataDb.db.Updateable(list2).ExecuteCommand();
                if (count!= list2.Count)
                {
                    DataDb.db.RollbackTran();
                    return "错误:更新全线视觉确认的数据失败";
                }
                DataDb.db.CommitTran();
                return $"成功:{msg}";
            }
            catch (Exception ee)
            {
                DataDb.db.RollbackTran();
                result = "错误:" + ee.Message;
            }
            return result;

        }
        internal static string MLVisioninfo(string[] vision)
        {
            DataDb.db.BeginTran();
            string result = "";
            try
            {
                string ms = "";
                string msg = "";
                if (vision.Length != 30)
                {
                    return "错误:视觉查询数量错误";
                }
                List<HTZD_Assemble> list2 = DataDb.db.Queryable<HTZD_Assemble>().Where(it => it.PlatingStatus == "在tray盘").OrderBy(it => it.id, OrderByType.Asc).ToList();
                if (list2.Count != 30)
                {
                    result = $"错误:在tray盘中的数量为{list2.Count},数量错误";
                    return result;
                }
                for (int i = 0; i < 30; i++)
                {
                    int Lower = i / 5;
                    int arry = i % 5;
                    if (vision[i] == "0")
                    {
                        if (list2[i].WriteCodeResult != "NG")
                        {
                            ms = ms + "\r\n" + $"第{Lower + 1}行{arry + 1}列编码{list2[i].barCode}错误，视觉为空，数据库为OK";
                        }
                    }
                    else
                    {
                        if (list2[i].WriteCodeResult != "OK")
                        {
                            ms = ms + "\r\n" + $"第{Lower + 1}行{arry + 1}列编码{list2[i].barCode}错误，视觉不为空，数据库不为NG";
                        }
                    }
                    msg = msg + "\r\n" + $"第{Lower + 1}行{arry + 1}列编码{list2[i].barCode}";
                    list2[i].PlatingStatus = "tray盘确认";
                }
                if (ms != "")
                {
                    result = "错误:" + ms;
                    return result;
                }
                int count = DataDb.db.Updateable(list2).ExecuteCommand();
                if (count != list2.Count)
                {
                    DataDb.db.RollbackTran();
                    return "错误:更新全线视觉确认的数据失败";
                }
                DataDb.db.CommitTran();
                return $"成功:{msg}";
            }
            catch (Exception ee)
            {
                DataDb.db.RollbackTran();
                result = "错误:" + ee.Message;
            }
            return result;

        }
        internal static List<HTZD_Assemble> GETVisioninfo1()
        {
            try
            {
                string ms = "";
                string msg = "";
                List<HTZD_Assemble> list2 = DataDb.db.Queryable<HTZD_Assemble>().Where(it => it.PlatingStatus == "在tray盘").OrderBy(it => it.id, OrderByType.Asc).ToList();
                if (list2.Count==0)
                {
                    return list2;
                }
                string getduibi = duibi(list2[0].barCode, list2[list2.Count-1].barCode, LocalFile.Config.ischeck, list2.Count-1);
                if (getduibi != "OK")
                {
                    TextBoxLog.AddLog($"查询tray盘错误,{getduibi}");
                    return null;
                }
                return list2;
            }
            catch (Exception ee)
            {
                string result = "错误:" + ee.Message;
                TextBoxLog.AddLog($"查询tray盘错误,{ee.Message}");
                return null;
            }
        }
        internal static string UpdateVisioninfo1(List<HTZD_Assemble> visiion)
        {
            DataDb.db.BeginTran();
            try
            {
                int count = DataDb.db.Updateable(visiion).ExecuteCommand();
                if (count != visiion.Count)
                {
                    DataDb.db.RollbackTran();
                    return "错误:更新全线视觉确认的数据失败";
                }
                return "OK";
            }
            catch (Exception ee)
            {
                DataDb.db.RollbackTran();
                string result = "错误:" + ee.Message;
                return result;
                //Logs.Write("更新全线视觉确认的数据失败：" + ee.Message);
            }
        }/// <summary>
         /// 到料数量
         /// </summary>
         /// <param name="visiion"></param>
         /// <returns></returns>
        internal static List<HTZD_Assemble> GetdtartInfo()
        {
            try
            {
                //List<HTZD_Assemble> list2 = DataDb.db.Queryable<HTZD_Assemble>().Where(it => it.PlatingStatus != null && it.PlatingStatus != "待供料" && it.PlatingStatus != "tray盘确认" && it.PlatingStatus != "下料" && it.PlatingStatus != "待下料").OrderBy(it => it.id, OrderByType.Desc).Take(30).ToList();
                List<HTZD_Assemble> list2 = new List<HTZD_Assemble>();
                int count = DataDb.db.Queryable<HTZD_Assemble>().Where(it => it.PlatingStatus == "在tray盘").Count();
                if (count==30)
                {
                    
                       list2 =DataDb.db.Queryable<HTZD_Assemble>().Where(it => it.PlatingStatus != null && it.PlatingStatus != "待供料" && it.PlatingStatus != "在tray盘" && it.PlatingStatus != "tray盘确认" && it.PlatingStatus != "下料" && it.PlatingStatus != "待下料").OrderBy(it => it.id, OrderByType.Desc).Take(30).ToList();
                }
                else
                {
                     list2 = DataDb.db.Queryable<HTZD_Assemble>().Where(it => it.PlatingStatus != null && it.PlatingStatus != "待供料" && it.PlatingStatus != "tray盘确认" && it.PlatingStatus != "下料" && it.PlatingStatus != "待下料").OrderBy(it => it.id, OrderByType.Desc).Take(30).ToList();
                }

                return list2;
            }
            catch (Exception ee)
            {
                string result = "错误:" + ee.Message;
                TextBoxLog.AddLog($"获取到料信息失败,{ee.Message}");
                return null;
            }
        }
        /// <summary>
        /// 当天过站数量
        /// </summary>
        /// <param name="visiion"></param>
        /// <returns></returns>
        internal static int GettodayInfo()
        {
            try
            {
                //var list = DataDb.db.Queryable<HTZD_Assemble>().Where(it => (it.PlatingStatus == "下料" || it.PlatingStatus == "待下料")&&it.PlatingTime>DateTime.Today).ToList();
                return DataDb.db.Queryable<HTZD_Assemble>().Where(it => (it.PlatingStatus == "下料" || it.PlatingStatus == "待下料")&&it.PlatingTime>DateTime.Today).Count();

            }
            catch (Exception ee)
            {
                string result = "错误:" + ee.Message;
                TextBoxLog.AddLog($"获取到料信息失败,{ee.Message}");
                return -1;
            }
        }/// <summary>
         /// 当天过站合格数量
         /// </summary>
         /// <param name="visiion"></param>
         /// <returns></returns>
        internal static int GettodayPassInfo()
        {
            try
            {
                //var kls = DataDb.db.Queryable<HTZD_Assemble>().Where(it => (it.PlatingStatus == "下料" || it.PlatingStatus == "待下料")&&it.PlatingTime>DateTime.Today&&it.WriteCodeStatus=="OK").ToList();
                return DataDb.db.Queryable<HTZD_Assemble>().Where(it => (it.PlatingStatus == "下料" || it.PlatingStatus == "待下料")&&it.PlatingTime>DateTime.Today&&it.WriteCodeResult=="OK").Count();

            }
            catch (Exception ee)
            {
                string result = "错误:" + ee.Message;
                TextBoxLog.AddLog($"获取到料信息失败,{ee.Message}");
                return -1;
            }
        }
    }
}
